package com.yztc.youbadbad.helper;

import android.content.Context;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
 * Created by Tiffany.V.Chou on 2016/11/15.
 */

public class AssetHelper {
    // 从assets 文件夹中获取文件并读取数据
    public static String getFromAssets(Context context, String fileName) {
        String result = "";
        BufferedInputStream bis = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            InputStream is = context.getResources().getAssets().open(fileName);
            bis = new BufferedInputStream(is);
            int c = 0;
            byte[] buffer = new byte[1024 * 8];
            while ((c = bis.read(buffer)) != -1) {
                baos.write(buffer, 0, c);
                baos.flush();
            }
            result = baos.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (bis != null) {
                    bis.close();
                }
            } catch (Exception ex) {}
        }
        return result;
    }
}
